/* i2c - Example

   For other examples please check:
   https://github.com/espressif/esp-idf/tree/master/examples

   See README.md file to get detailed usage of this example.

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "sdkconfig.h"
#include "ds1307.h"
#include "eerom24c32.h"

/*sjdfklajfd*/

// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t seconds :4;
//             uint8_t ten_sec :3;
//             uint8_t ch :1;
//         };
        
//     };
// }second_data_TypeDef;

// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t minutes :4;
//             uint8_t ten_min :3;
//             uint8_t bit7 :1;
//         };
        
//     };
// }minutes_data_TypeDef;

// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t hours :4;
//             uint8_t ten_hour :2;
//             uint8_t jinzhi :1;
//             uint8_t :1;
//         };
        
//     };
// }hours_data_TypeDef;

// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t week :3;
//             uint8_t :5;
//         };
        
//     };
// }weeks_data_TypeDef;

// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t date :4;
//             uint8_t ten_date:2;
//             uint8_t :2;
//         };
        
//     };
// }dates_data_TypeDef;


// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t month :4;
//             uint8_t ten_month:1;
//             uint8_t :3;
//         };
        
//     };
// }months_data_TypeDef;

// typedef struct 
// {
//     union {
//         uint8_t value;
//         struct 
//         {
//             uint8_t year :4;
//             uint8_t ten_year:4;
//         };
//     };
// }years_data_TypeDef;

// second_data_TypeDef seconds;
// minutes_data_TypeDef minutes;
// hours_data_TypeDef hours;
// weeks_data_TypeDef weeks;
// dates_data_TypeDef dates;
// months_data_TypeDef months;
// years_data_TypeDef years;
static const char *TAG = "i2c-example";

// #define _I2C_NUMBER(num) I2C_NUM_##num
// #define I2C_NUMBER(num) _I2C_NUMBER(num)

// #define DATA_LENGTH 512                  /*!< Data buffer length of test buffer */
// #define RW_TEST_LENGTH 128               /*!< Data length for r/w test, [0,DATA_LENGTH] */
// #define DELAY_TIME_BETWEEN_ITEMS_MS 1000 /*!< delay time between different test items */

// #define I2C_SLAVE_SCL_IO (5)               /*!< gpio number for i2c slave clock */
// #define I2C_SLAVE_SDA_IO (4)               /*!< gpio number for i2c slave data */
// #define I2C_SLAVE_NUM I2C_NUMBER(1) /*!< I2C port number for slave dev */
// #define I2C_SLAVE_TX_BUF_LEN (2 * DATA_LENGTH)              /*!< I2C slave tx buffer size */
// #define I2C_SLAVE_RX_BUF_LEN (2 * DATA_LENGTH)              /*!< I2C slave rx buffer size */

// #define I2C_MASTER_SCL_IO (19)               /*!< gpio number for I2C master clock */
// #define I2C_MASTER_SDA_IO (18)               /*!< gpio number for I2C master data  */
// #define I2C_MASTER_NUM I2C_NUMBER (0) /*!< I2C port number for master dev */
// #define I2C_MASTER_FREQ_HZ 100000        /*!< I2C master clock frequency */
// #define I2C_MASTER_TX_BUF_DISABLE 0                           /*!< I2C master doesn't need buffer */
// #define I2C_MASTER_RX_BUF_DISABLE 0                           /*!< I2C master doesn't need buffer */

// #define BH1750_SENSOR_ADDR 0x68   /*!< slave address for BH1750 sensor */
// #define DS1307_RTC_ADDR 0x68   /*!< slave address for DS1307 sensor */
// #define EEPROM_24C32_ADDR 0x50   /*!< slave address for DS1307 sensor */
// #define BH1750_CMD_START CONFIG_BH1750_OPMODE   /*!< Operation mode */
// #define ESP_SLAVE_ADDR 0xa0 /*!< ESP32 slave address, you can set any 7bit value */
// #define WRITE_BIT I2C_MASTER_WRITE              /*!< I2C master write */
// #define READ_BIT I2C_MASTER_READ                /*!< I2C master read */
// #define ACK_CHECK_EN 0x1                        /*!< I2C master will check ack from slave*/
// #define ACK_CHECK_DIS 0x0                       /*!< I2C master will not check ack from slave */
// #define ACK_VAL 0x0                             /*!< I2C ack value */
// #define NACK_VAL 0x1                            /*!< I2C nack value */

// SemaphoreHandle_t print_mux = NULL;

// static esp_err_t DS1307_set_time(i2c_port_t i2c_num, uint8_t year,uint8_t month,uint8_t date,uint8_t week,uint8_t hour ,uint8_t minute,uint8_t second);
// /**
//  * @brief test code to read esp-i2c-slave
//  *        We need to fill the buffer of esp slave device, then master can read them out.
//  *
//  * _______________________________________________________________________________________
//  * | start | slave_addr + rd_bit +ack | read n-1 bytes + ack | read 1 byte + nack | stop |
//  * --------|--------------------------|----------------------|--------------------|------|
//  *
//  * @note cannot use master read slave on esp32c3 because there is only one i2c controller on esp32c3
//  */
// static esp_err_t __attribute__((unused)) i2c_master_read_slave(i2c_port_t i2c_num, uint8_t *data_rd, size_t size)
// {
//     if (size == 0) {
//         return ESP_OK;
//     }
//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, (ESP_SLAVE_ADDR << 1) | READ_BIT, ACK_CHECK_EN);
//     if (size > 1) {
//         i2c_master_read(cmd, data_rd, size - 1, ACK_VAL);
//     }
//     i2c_master_read_byte(cmd, data_rd + size - 1, NACK_VAL);
//     i2c_master_stop(cmd);
//     esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     return ret;
// }

// /**
//  * @brief Test code to write esp-i2c-slave
//  *        Master device write data to slave(both esp32),
//  *        the data will be stored in slave buffer.
//  *        We can read them out from slave buffer.
//  *
//  * ___________________________________________________________________
//  * | start | slave_addr + wr_bit + ack | write n bytes + ack  | stop |
//  * --------|---------------------------|----------------------|------|
//  *
//  * @note cannot use master write slave on esp32c3 because there is only one i2c controller on esp32c3
//  */
// static esp_err_t __attribute__((unused)) i2c_master_write_slave(i2c_port_t i2c_num, uint8_t *data_wr, size_t size)
// {
//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, (ESP_SLAVE_ADDR << 1) | WRITE_BIT, ACK_CHECK_EN);
//     i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN);
//     i2c_master_stop(cmd);
//     esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     return ret;
// }

// /**
//  * @brief test code to operate on BH1750 sensor
//  *
//  * 1. set operation mode(e.g One time L-resolution mode)
//  * _________________________________________________________________
//  * | start | slave_addr + wr_bit + ack | write 1 byte + ack  | stop |
//  * --------|---------------------------|---------------------|------|
//  * 2. wait more than 24 ms
//  * 3. read data
//  * ______________________________________________________________________________________
//  * | start | slave_addr + rd_bit + ack | read 1 byte + ack  | read 1 byte + nack | stop |
//  * --------|---------------------------|--------------------|--------------------|------|
//  */
// static esp_err_t i2c_master_sensor_test(i2c_port_t i2c_num, uint8_t *data_h, uint8_t *data_l)
// {
//     int ret;
//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     // i2c_master_start(cmd);
//     // i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
//     // i2c_master_write_byte(cmd, BH1750_CMD_START, ACK_CHECK_EN);
//     // i2c_master_stop(cmd);
//     // ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     // i2c_cmd_link_delete(cmd);
//     // if (ret != ESP_OK) {
//     //     return ret;
//     // }
//     // vTaskDelay(30 / portTICK_RATE_MS);
//     cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
//     i2c_master_read_byte(cmd, data_h, ACK_VAL);
//     i2c_master_read_byte(cmd, data_l, NACK_VAL);
//     i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     return ret;
// }

// static esp_err_t i2c_master_read_ds1307(i2c_port_t i2c_num )
// {
//     int ret,i;
//     uint8_t data[8]= {0};
//     uint8_t sec,min,hour,day,date,month,year,data0,data1;
//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, DS1307_RTC_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
//     // i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
//     i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     if (ret != ESP_OK) {
//         return ret;
//     }
//     vTaskDelay(30 / portTICK_RATE_MS);

//     cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, DS1307_RTC_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
//     // vTaskDelay(1/portTICK_PERIOD_MS);
//     i2c_master_read_byte(cmd, &seconds.value, ACK_VAL);
//     i2c_master_read_byte(cmd, &minutes.value, ACK_VAL);
//     i2c_master_read_byte(cmd, &hours.value, ACK_VAL);
//     i2c_master_read_byte(cmd, &weeks.value, ACK_VAL);
//     i2c_master_read_byte(cmd, &dates.value, ACK_VAL);
//     i2c_master_read_byte(cmd, &months.value, ACK_VAL);
//     i2c_master_read_byte(cmd, &years.value, NACK_VAL);
//     i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     printf("0x%x  0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n",seconds.value,minutes.value,hours.value,weeks.value,dates.value,months.value,years.value);
//     printf("%d-%d-%d-%d %d:%d:%d\r\n",(years.ten_year*10+years.year+2000),(months.ten_month*10+months.month),
//     (dates.ten_date*10+dates.date),(weeks.week),(hours.ten_hour*10+hours.hours),(minutes.ten_min*10+minutes.minutes),
//     (seconds.ten_sec*10+seconds.seconds));


//     // if(years.value==0)
//     // {
//     //     DS1307_set_time(i2c_num,21,8,18,3,13,20,0);
//     //     vTaskDelay(500/portTICK_PERIOD_MS);
//     // }

//     return ret;
// }

// static esp_err_t DS1307_set_time(i2c_port_t i2c_num, uint8_t year,uint8_t month,uint8_t date,uint8_t week,uint8_t hour ,uint8_t minute,uint8_t second)
// {
//     int ret;
//     second_data_TypeDef seconds;
//     minutes_data_TypeDef minutes;
//     hours_data_TypeDef hours;
//     weeks_data_TypeDef weeks;
//     dates_data_TypeDef dates;
//     months_data_TypeDef months;
//     years_data_TypeDef years;
//     years.ten_year = year/10;
//     years.year = year%10;
//     months.ten_month = month/10;
//     months.month = month%10;
//     dates.ten_date = date/10;
//     dates.date = date%10;
//     weeks.week = week;
//     hours.ten_hour = hour/10;
//     hours.hours = hour%10;
//     minutes.ten_min = minute/10;
//     minutes.minutes = minute%10;
//     seconds.ten_sec = second/10;
//     seconds.seconds = second%10;
//     seconds.ch = 0;
//     printf("years value:%d %d %d \r\n",years.value,years.ten_year,years.year);

//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, DS1307_RTC_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, seconds.value, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, minutes.value, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, hours.value, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, weeks.value, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, dates.value, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, months.value, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, years.value, ACK_CHECK_EN);
//     // i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
//     i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     if (ret != ESP_OK) {
//         return ret;
//     }
//     else
//     {
//         return ESP_OK;
//     }


// }

// static esp_err_t eeprom_24c32_write(i2c_port_t i2c_num, uint16_t data_addr,uint8_t * data)
// {
//     int ret;
//     printf("*****write address is : 0x%x  ;data length is %d \r\n",data_addr,sizeof(data));

//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, EEPROM_24C32_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, (data_addr>>8), ACK_CHECK_EN);  //send high addr xxxxoooo ooooo
//     i2c_master_write_byte(cmd, (data_addr), ACK_CHECK_EN); //send low addr 32K only need 12bit address
//     i2c_master_write_byte(cmd, data[0], ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, data[1], ACK_CHECK_EN);
//     // i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
//     i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     if (ret != ESP_OK) {
//         return ret;
//     }
//     else
//     {
//         return ESP_OK;
//     }
// }

// static esp_err_t eeprom_24c32_read(i2c_port_t i2c_num ,uint16_t addr,uint16_t length)
// {
//     int ret=ESP_OK;
//     uint8_t data[8]= {0};
//     uint8_t sec,min,hour,day,date,month,year,data0,data1;
//     i2c_cmd_handle_t cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, EEPROM_24C32_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, (addr>>8), ACK_CHECK_EN);
//     i2c_master_write_byte(cmd, (addr), ACK_CHECK_EN);
//     // i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
//     // i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     if (ret != ESP_OK) {
//         return ret;
//     }
//     vTaskDelay(30 / portTICK_RATE_MS);

//     cmd = i2c_cmd_link_create();
//     i2c_master_start(cmd);
//     i2c_master_write_byte(cmd, EEPROM_24C32_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
//     // vTaskDelay(1/portTICK_PERIOD_MS);
//     i2c_master_read_byte(cmd, &data[0], ACK_VAL);
//     i2c_master_read_byte(cmd, &data[1], NACK_VAL);
//     i2c_master_stop(cmd);
//     ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
//     i2c_cmd_link_delete(cmd);
//     printf("****read from eeprom 24c32 aadr:0x%x \r\n",addr);
//     printf("data is 0x%x 0x%x \r\n",data[0],data[1]);

//     return ret;
// }
// /**
//  * @brief i2c master initialization
//  */
// static esp_err_t i2c_master_init(void)
// {
//     int i2c_master_port = I2C_MASTER_NUM;
//     i2c_config_t conf = {
//         .mode = I2C_MODE_MASTER,
//         .sda_io_num = I2C_MASTER_SDA_IO,
//         .sda_pullup_en = GPIO_PULLUP_ENABLE,
//         .scl_io_num = I2C_MASTER_SCL_IO,
//         .scl_pullup_en = GPIO_PULLUP_ENABLE,
//         .master.clk_speed = I2C_MASTER_FREQ_HZ,
//         // .clk_flags = 0,          /*!< Optional, you can use I2C_SCLK_SRC_FLAG_* flags to choose i2c source clock here. */
//     };
//     esp_err_t err = i2c_param_config(i2c_master_port, &conf);
//     if (err != ESP_OK) {
//         return err;
//     }
//     return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
// }

// #if !CONFIG_IDF_TARGET_ESP32C3
// /**
//  * @brief i2c slave initialization
//  */
// static esp_err_t i2c_slave_init(void)
// {
//     int i2c_slave_port = I2C_SLAVE_NUM;
//     i2c_config_t conf_slave = {
//         .sda_io_num = I2C_SLAVE_SDA_IO,
//         .sda_pullup_en = GPIO_PULLUP_ENABLE,
//         .scl_io_num = I2C_SLAVE_SCL_IO,
//         .scl_pullup_en = GPIO_PULLUP_ENABLE,
//         .mode = I2C_MODE_SLAVE,
//         .slave.addr_10bit_en = 0,
//         .slave.slave_addr = ESP_SLAVE_ADDR,
//     };
//     esp_err_t err = i2c_param_config(i2c_slave_port, &conf_slave);
//     if (err != ESP_OK) {
//         return err;
//     }
//     return i2c_driver_install(i2c_slave_port, conf_slave.mode, I2C_SLAVE_RX_BUF_LEN, I2C_SLAVE_TX_BUF_LEN, 0);
// }

// /**
//  * @brief test function to show buffer
//  */
// static void disp_buf(uint8_t *buf, int len)
// {
//     int i;
//     for (i = 0; i < len; i++) {
//         printf("%02x ", buf[i]);
//         if ((i + 1) % 16 == 0) {
//             printf("\n");
//         }
//     }
//     printf("\n");
// }
// #endif //!CONFIG_IDF_TARGET_ESP32C3

// static void i2c_test_task(void *arg)
// {
//     int ret;
//     uint32_t task_idx = (uint32_t)arg;
// #if !CONFIG_IDF_TARGET_ESP32C3
//     int i = 0;
//     uint8_t *data = (uint8_t *)malloc(DATA_LENGTH);
//     uint8_t *data_wr = (uint8_t *)malloc(DATA_LENGTH);
//     uint8_t *data_rd = (uint8_t *)malloc(DATA_LENGTH);
// #endif //!CONFIG_IDF_TARGET_ESP32C3
//     uint8_t sensor_data_h, sensor_data_l;
//     int cnt = 0;
//     while (1) {
//         ESP_LOGI(TAG, "TASK[%d] test cnt: %d", task_idx, cnt++);
//         ret = i2c_master_sensor_test(I2C_MASTER_NUM, &sensor_data_h, &sensor_data_l);
//         xSemaphoreTake(print_mux, portMAX_DELAY);
//         if (ret == ESP_ERR_TIMEOUT) {
//             ESP_LOGE(TAG, "I2C Timeout");
//         } else if (ret == ESP_OK) {
//             printf("*******************\n");
//             printf("TASK[%d]  MASTER READ SENSOR( BH1750 )\n", task_idx);
//             printf("*******************\n");
//             printf("data_h: %02x\n", sensor_data_h);
//             printf("data_l: %02x\n", sensor_data_l);
//             printf("sensor val: %.02f [Lux]\n", (sensor_data_h << 8 | sensor_data_l) / 1.2);
//         } else {
//             ESP_LOGW(TAG, "%s: No ack, sensor not connected...skip...", esp_err_to_name(ret));
//         }
//         xSemaphoreGive(print_mux);
//         vTaskDelay((DELAY_TIME_BETWEEN_ITEMS_MS * (task_idx + 1)) / portTICK_RATE_MS);
//         //---------------------------------------------------
// #if !CONFIG_IDF_TARGET_ESP32C3
//         for (i = 0; i < DATA_LENGTH; i++) {
//             data[i] = i;
//         }
//         xSemaphoreTake(print_mux, portMAX_DELAY);
//         size_t d_size = i2c_slave_write_buffer(I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
//         if (d_size == 0) {
//             ESP_LOGW(TAG, "i2c slave tx buffer full");
//             ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, DATA_LENGTH);
//         } else {
//             ret = i2c_master_read_slave(I2C_MASTER_NUM, data_rd, RW_TEST_LENGTH);
//         }

//         if (ret == ESP_ERR_TIMEOUT) {
//             ESP_LOGE(TAG, "I2C Timeout");
//         } else if (ret == ESP_OK) {
//             printf("*******************\n");
//             printf("TASK[%d]  MASTER READ FROM SLAVE\n", task_idx);
//             printf("*******************\n");
//             printf("====TASK[%d] Slave buffer data ====\n", task_idx);
//             disp_buf(data, d_size);
//             printf("====TASK[%d] Master read ====\n", task_idx);
//             disp_buf(data_rd, d_size);
//         } else {
//             ESP_LOGW(TAG, "TASK[%d] %s: Master read slave error, IO not connected...\n",
//                      task_idx, esp_err_to_name(ret));
//         }
//         xSemaphoreGive(print_mux);
//         vTaskDelay((DELAY_TIME_BETWEEN_ITEMS_MS * (task_idx + 1)) / portTICK_RATE_MS);
//         //---------------------------------------------------
//         int size;
//         for (i = 0; i < DATA_LENGTH; i++) {
//             data_wr[i] = i + 10;
//         }
//         xSemaphoreTake(print_mux, portMAX_DELAY);
//         //we need to fill the slave buffer so that master can read later
//         ret = i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
//         if (ret == ESP_OK) {
//             size = i2c_slave_read_buffer(I2C_SLAVE_NUM, data, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
//         }
//         if (ret == ESP_ERR_TIMEOUT) {
//             ESP_LOGE(TAG, "I2C Timeout");
//         } else if (ret == ESP_OK) {
//             printf("*******************\n");
//             printf("TASK[%d]  MASTER WRITE TO SLAVE\n", task_idx);
//             printf("*******************\n");
//             printf("----TASK[%d] Master write ----\n", task_idx);
//             disp_buf(data_wr, RW_TEST_LENGTH);
//             printf("----TASK[%d] Slave read: [%d] bytes ----\n", task_idx, size);
//             disp_buf(data, size);
//         } else {
//             ESP_LOGW(TAG, "TASK[%d] %s: Master write slave error, IO not connected....\n",
//                      task_idx, esp_err_to_name(ret));
//         }
//         xSemaphoreGive(print_mux);
//         vTaskDelay((DELAY_TIME_BETWEEN_ITEMS_MS * (task_idx + 1)) / portTICK_RATE_MS);
// #endif //!CONFIG_IDF_TARGET_ESP32C3
//     }
//     vSemaphoreDelete(print_mux);
//     vTaskDelete(NULL);
// }

void app_main(void)
{
    uint8_t data[4]={0xb0,0xb1,0xb2,0xb3};
    uint8_t readeerom[4],readtime[7];
    // print_mux = xSemaphoreCreateMutex();
#if !CONFIG_IDF_TARGET_ESP32C3
    // ESP_ERROR_CHECK(i2c_slave_init());
#endif
    // ESP_ERROR_CHECK(i2c_master_init());
    eerom24c32_i2c_master_init();
    eerom24c32_write_data(0x00,data,sizeof(data));
    vTaskDelay(10/portTICK_PERIOD_MS);
    eerom24c32_read_data(0x00,readeerom,sizeof(readeerom));

    printf("read eerom data:");
    for(int i = 0; i<4;i++)
    {
        printf("0x%x " ,readeerom[i]);
    }
    printf("\r\n");

    ds1307_i2c_master_init();


    //eeprom write test
    // eeprom_24c32_write(I2C_MASTER_NUM,0x0000,data);
    // vTaskDelay(10/portTICK_PERIOD_MS);
    // eeprom_24c32_read(I2C_MASTER_NUM,0x0000,2);

// fist set time default values 2021-8-18-3 14:15
    // DS1307_set_time(I2C_MASTER_NUM,21,8,18,3,14,15,0);
    while (1)
    {
        ds1307_read_data(readtime,7);
        printf("%d-%d-%d-%d %d:%d:%d\r\n",2000+readtime[6],readtime[5],readtime[4],readtime[3],
        readtime[2],readtime[1],readtime[0]);
        // i2c_master_read_ds1307(I2C_MASTER_NUM);
        vTaskDelay(1000/portTICK_PERIOD_MS);
        /* code */
    }
    
    // xTaskCreate(i2c_test_task, "i2c_test_task_0", 1024 * 2, (void *)0, 10, NULL);
    // xTaskCreate(i2c_test_task, "i2c_test_task_1", 1024 * 2, (void *)1, 10, NULL);
}
